﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKRevitBase.WinForm;
using YJKRevitView;
using YJKInspectModel.ModelValidation.ClearHeightCheck;

namespace YJKInspectModel.ModelValidation.Commands
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class ClearHeightCheckCmd : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			if (!(commandData.Application.ActiveUIDocument.Document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != Autodesk.Revit.UI.Result.Succeeded)
				{
					return result;
				}
			}
			ClearHeightCheckFun clearHeightCheckFun = new ClearHeightCheckFun(application);
			List<Element> list = new List<Element>();
			list = clearHeightCheckFun.getAllElements();
			if (ClearHeightCheckCmd._hWndRevit == null)
			{
                ClearHeightCheckCmd._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			ClearHeightCheckApplication.m_ThisApp.InitSelectEle(list);
			CheckTypeSelectForm checkTypeSelectForm = new CheckTypeSelectForm(list);
			if (checkTypeSelectForm.ShowDialog() == DialogResult.OK)
			{
				if (checkTypeSelectForm.getSelectedType() == 0)
				{
					List<List<CurveLoop>> list2 = new List<List<CurveLoop>>();
					list2 = ApplicationClearHeight.thisApp.DrawZoneManualOne(application, ClearHeightCheckCmd._hWndRevit);
					if (list2.Count < 1)
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					clearHeightCheckFun.setSelectType(false);
					clearHeightCheckFun.setSelectFloor(false);
					clearHeightCheckFun.setUserSelectedArea(list2);
				}
				else if (1 == checkTypeSelectForm.getSelectedType())
				{
					List<FilledRegion> list3 = new List<FilledRegion>();
					list3 = this.SelectFillRegion(activeUIDocument);
					if (list3.Count < 1)
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					List<List<CurveLoop>> list4 = new List<List<CurveLoop>>();
					foreach (FilledRegion filledRegion in list3)
					{
						List<CurveLoop> item = new List<CurveLoop>();
						item = filledRegion.GetBoundaries().ToList<CurveLoop>();
						list4.Add(item);
					}
					clearHeightCheckFun.setSelectType(false);
					clearHeightCheckFun.setSelectFloor(false);
					clearHeightCheckFun.setUserSelectedArea(list4);
				}
				else if (2 == checkTypeSelectForm.getSelectedType())
				{
					new List<Level>();
					FloorLevelSelectForm floorLevelSelectForm = new FloorLevelSelectForm(clearHeightCheckFun.GetCurDocLevels());
					List<Level> list5 = new List<Level>();
					if (floorLevelSelectForm.ShowDialog() != DialogResult.OK)
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					list5 = floorLevelSelectForm.getSelectedLevelList();
					if (list5.Count < 1)
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					clearHeightCheckFun.setSelectType(true);
					clearHeightCheckFun.setSelectFloor(false);
					clearHeightCheckFun.setSelectedLevelList(list5);
				}
				else if (3 == checkTypeSelectForm.getSelectedType())
				{
					List<List<CurveLoop>> list6 = new List<List<CurveLoop>>();
					list6 = ApplicationClearHeight.thisApp.GetRoomLoops(activeUIDocument);
					if (list6.Count < 1)
					{
						YJKMessageBox.Show("获取房间边界出错");
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					clearHeightCheckFun.setSelectType(false);
					clearHeightCheckFun.setSelectFloor(false);
					clearHeightCheckFun.setUserSelectedArea(list6);
				}
				else if (4 == checkTypeSelectForm.getSelectedType())
				{
					List<List<CurveLoop>> list7 = new List<List<CurveLoop>>();
					list7 = ApplicationClearHeight.thisApp.GetFloorLoops(activeUIDocument);
					if (list7.Count < 1)
					{
						YJKMessageBox.Show("获取楼板边界出错");
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					clearHeightCheckFun.setSelectType(false);
					clearHeightCheckFun.setSelectFloor(true);
					clearHeightCheckFun.setUserSelectedArea(list7);
				}
				if (!clearHeightCheckFun.StartCheckClearHeight())
				{
					return Autodesk.Revit.UI.Result.Failed;
				}
				ApplicationClearHeight.thisApp.CreateClearHeightForm(clearHeightCheckFun, ClearHeightCheckCmd._hWndRevit);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private List<FilledRegion> SelectFillRegion(UIDocument uiDoc)
		{
			List<FilledRegion> list = new List<FilledRegion>();
			RevitVersionFuncs.ClearSelection(uiDoc.Selection);
			List<FilledRegion> result;
			try
			{
				IList<Reference> list2 = uiDoc.Selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new FilledRegionFilter(), "请选择要检测的区域");
				List<Reference> list3 = new List<Reference>();
				if (list2.Count > 0)
				{
					list3 = list2.ToList<Reference>();
					if (list3.Count == 0)
					{
						TaskDialog.Show("盈建科提示", "未选区域，请重新选择。");
						result = list;
					}
					else
					{
						list = (from pickObj in list3
						select uiDoc.Document.GetElement(pickObj.ElementId) as FilledRegion).ToList<FilledRegion>();
						result = list;
					}
				}
				else
				{
					result = list;
				}
			}
			catch
			{
				result = list;
			}
			return result;
		}

		public static WindowHandle _hWndRevit;
	}
}
